﻿@page "/Formula"
@using BlazorDatasheet.Core.Data
@using BlazorDatasheet.Core.Formats.DefaultConditionalFormats
@using System.Drawing
@using BlazorDatasheet.DataStructures.Geometry

<p>Blazor datasheet has support for an excel-like formula language.</p>

<p>This example shows a 10x10 sheet with sin functions applied to each. The phase can be controlled with the formula variable "x", which is set via the range slider below.</p>

<input type="range" min="0" max="10" step="0.05" @oninput="@(e => Sheet.FormulaEngine.SetVariable("x", e.Value))"/>

<Datasheet Sheet="Sheet" ShowColHeadings="false" ShowRowHeadings="false"></Datasheet>

@code{

    public Sheet Sheet;

    protected override void OnInitialized()
    {
        Sheet = new Sheet(10, 10);
        Sheet.BatchUpdates();
        Sheet.Commands.PauseHistory();
        Sheet.Columns.SetWidth(0, 10, 40);
        Sheet.Rows.SetHeight(0, 10, 40);

        for (int i = 0; i < 10; i++)
        {
            for (int j = 0; j < 10; j++)
            {
                Sheet.Cells.SetFormula(i, j, $"=(1+sin(({i / 10.0}+{j / 10.0})*3.14-x))/2");
            }
        }
        var colorCf = new NumberScaleConditionalFormat(Color.LightBlue, Color.GreenYellow);
        Sheet.ConditionalFormats.Apply(new Region(0, 10, 0, 10), colorCf);
        Sheet.FormulaEngine.SetVariable("x", 10);
        Sheet.EndBatchUpdates();
        Sheet.Commands.ResumeHistory();
    }

}